<template>
<div class="col-md-12">
  <auth-user-detail></auth-user-detail>
  <my-navbar>
    <template slot="right"> 
      <button class="btn btn-default" @click.prevent="onAdd" v-if="isShowAdd">
        <span class="glyphicon glyphicon-plus"></span> {{$t('common.add')}}
      </button>
      <button class="btn btn-default" @click.prevent="onRefresh">
        <span class="glyphicon glyphicon-refresh"></span> {{$t('common.refresh')}}
      </button>            
    </template> 
  </my-navbar>
  <div class="col-md-12">
    <my-table :cfg="cfg" :list="list" :loading="user.loading"></my-table>
  </div>
</div>
</template>

<script>
import _ from 'lodash'
import ebus from 'libs/ebus.js'
import toast from 'libs/toast.js'
import msgbox from 'libs/msgbox.js'
import {printObj} from 'libs/utils.js'
import locales from '../locales/authUser.js'
import AuthUserDetail from './AuthUserDetail.vue'
import {List} from '../configs/authUser.js'
import {http} from 'share/server/api.js'
import * as actions from 'share/server/vuex/actions/system.js'

export default {
  locales,
  components: {
    'auth-user-detail': AuthUserDetail,
  },
  vuex: {
    getters: {
      username: (state) => state.auth.username,
      client: (state) => state.auth.client,
      user: (state) => state.auth.user
    },
    actions
  },
  computed: {
    isAdmin () {
      return this.username === 'root'
    },
    isWebSysAdmin () {
      return this.client.role === 'webSysAdmin'
    },
    isWebSecAdmin () {
      return this.client.role === 'webSecAdmin'
    },
    isShowAdd () {
      return this.isAdmin || this.isWebSysAdmin
    },
    list () {
      return _.filter(this.user.list, (item) => {
        return item.role && item.role.startsWith('web')
      })
    }
  },
  data () {
    return {
      cfg: {
        checkable: false,
        showHead: true,
        columns: new List(this),
        operation: {
          name: this.$t("common.operation"),
          width: 'auto',
          actions: [{
            name: this.$t("operation.requestEnable"),
            doClick: this.onRequestEnable,
            isShow: this.isShowRequestEnable
          }, {
            name: this.$t("operation.requestDisable"),
            doClick: this.onRequestDisable,
            isShow: this.isShowRequestDisable
          }, {
            name: this.$t("operation.approveEnable"),
            doClick: this.onApproveEnable,
            isShow: this.isShowApproveEnable
          }, {
            name: this.$t("operation.approveDisable"),
            doClick: this.onApproveDisable,
            isShow: this.isShowApproveDisable
          }, {
            icon: 'glyphicon glyphicon-edit',
            doClick: this.onShowDetail,
            isShow: this.isShowEdit
          }, {
            icon: 'glyphicon glyphicon-trash',
            doClick: this.onDel,
            isShow: this.isShowDel
          }]
        }        
      }
    }
  },
  methods: {
    onRequestEnable (item) {
      http(`/api/auth/users/${item._id}/requestEnable`, 'POST').then(function(res) {
        if (res && res.data && res.data.code === 0) {
          toast.s(this, this.$t('common.success'), this.$t('msg.sendCmdSuccess'))
          this.doLoadUsers()
        } else {
          toast.code(this, res.data)
        }
      }.bind(this))
    },
    isShowRequestEnable (item) {
      if ((!item.isBuildIn)
        && (!item.enable)
        && (!item.spStatus)
        && (this.isWebSysAdmin)) {
        return true
      } else {
        return false
      }
    },
    onRequestDisable (item) {
      http(`/api/auth/users/${item._id}/requestDisable`, 'POST').then(function(res) {
        if (res && res.data && res.data.code === 0) {
          toast.s(this, this.$t('common.success'), this.$t('msg.sendCmdSuccess'))
          this.doLoadUsers()
        } else {
          toast.code(this, res.data)
        }
      }.bind(this))
    },
    isShowRequestDisable (item) {
      if ((!item.isBuildIn)
        && (item.enable)
        && (!item.spStatus)
        && (this.isWebSysAdmin)) {
        return true
      } else {
        return false
      }
    },
    onApproveEnable (item) {
      http(`/api/auth/users/${item._id}/approveEnable`, 'POST').then(function(res) {
        if (res && res.data && res.data.code === 0) {
          toast.s(this, this.$t('common.success'), this.$t('msg.sendCmdSuccess'))
          this.doLoadUsers()
        } else {
          toast.code(this, res.data)
        }
      }.bind(this))
    },
    isShowApproveEnable (item) {
      if ((item.spStatus === 'enable')
        && (this.isWebSecAdmin || this.isAdmin)) {
        return true
      } else {
        return false
      }
    },
    onApproveDisable (item) {
      http(`/api/auth/users/${item._id}/approveDisable`, 'POST').then(function(res) {
        if (res && res.data && res.data.code === 0) {
          toast.s(this, this.$t('common.success'), this.$t('msg.sendCmdSuccess'))
          this.doLoadUsers()
        } else {
          toast.code(this, res.data)
        }
      }.bind(this))
    },
    isShowApproveDisable (item) {
      if ((item.spStatus === 'disable')
        && (this.isWebSecAdmin || this.isAdmin)) {
        return true
      } else {
        return false
      }
    },
    isShowDel (item) {
      if ((!item.isBuildIn)
        && (!item.enable)
        && (this.isWebSysAdmin || this.isAdmin)) {
        return true
      } else {
        return false
      }
    },
    isShowEdit (item) {
      if (this.isWebSysAdmin
        || this.isAdmin) {
        return true
      } else {
        return false
      }
    },
    onAdd () {
      this.onShowDetail({
        _id: -1,
        isBuildIn: 0,
        enable: 0,
        privilege: 2,
        authBy: 'password',
        role: 'webUser',
        username: '',
        password: '',
        maxNumOfConn: 1,
        secPolicyEnable: 1  
      })
    },
    onShowDetail (item) {
      ebus.emit('show-user-detail', item)
    },
    onRefresh () {
      this.doLoadUsers()
    },
    onDel (item) {
      msgbox.alert({
        title: this.$t('alert.areYouSure'),
        text: this.$t('alert.notBeRecovered'),
        type: "warning",
        showCancelButton: true,
        confirmButtonColor: "#DD6B55",
        cancelButtonText: this.$t('common.cancel'),
        confirmButtonText: this.$t('common.confirm'),
        closeOnConfirm: false        
      }, 
      function() {
        http(`/api/auth/users/${item._id}`, 'DELETE').then(function(res) {
          if (res && res.data && res.data.code === 0) {
            toast.s(this, this.$t('common.success'), this.$t('msg.sendCmdSuccess'))
            this.doLoadUsers()
          } else {
            toast.code(this, res.data)
          }
          msgbox.close()
        }.bind(this))
      }.bind(this))
    }
  }
}
</script>

<style>
</style>